<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/26
 * Time: 11:35
 * Desc: 导出管理
 */
namespace app\store\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use PHPExcel;
use PHPExcel_IOFactory;

class Export extends BasicAdmin{
    /**
     * @Author  lingyun
     * @Desc    导出原料信息
     * 2018-05-26
     */
    public function export_material(){
        $get = input('get.');
        $map['is_deleted'] = ['eq',0];
        $map['store_id'] = ['eq',session('user.store_id')];
        if(!empty($get['cate_1'])){
            $map['first_category'] = ['like',"%".$get['cate_1']."%"];
        }
        if(!empty($get['cate_2'])){
            $map['second_category'] = ['like',"%".$get['cate_2']."%"];
        }
        if(!empty($get['cate_3'])){
            $map['third_category'] = ['like',"%".$get['cate_3']."%"];
        }
        if(!empty($get['name'])){
            $map['name'] = ['like',"%".$get['name']."%"];
        }

        $data = db('goods_material')->where($map)->select();

        $filename = '原料详情'.date('YmdHis').'.xls';

        $objPHPExcel = new PHPExcel();//新建一个表
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("原料详情");
        $title = '原料详情 - '.date('Y-m-d',time());

        $objSheet->setCellValue('A1', $title);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');      //合并
        $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray(     //加粗和居中
            array(
                'font' => array (
                    'bold' => true
                ),
                'alignment' => array(
                    'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                )
            )
        );


        $objSheet
            ->setCellValue('A2', '原料编码')
            ->setCellValue('B2', '条码')
            ->setCellValue('C2', '原料名称')
            ->setCellValue('D2', '标准售价')
            ->setCellValue('E2', '单位')
            ->setCellValue('F2', '库存')
            ->setCellValue('G2', '一级分类')
            ->setCellValue('H2', '二级分类')
            ->setCellValue('I2', '三级分类');


        $j = 3;     //从第三行开始

        foreach ($data as $key => $value) {
            if(empty($value['res_num'])){
                $value['res_num'] = '未处理';
            }
            if(empty($value['dif_num'])){
                $value['dif_num'] = '未处理';
            }
            $objSheet
                ->setCellValue('A' . $j, $value['code'])
                ->setCellValue('B' . $j, $value['tiaoma'])
                ->setCellValue('C' . $j, $value['name'])
                ->setCellValue('D' . $j, $value['price'])
                ->setCellValue('E' . $j, $value['unit'])
                ->setCellValue('F' . $j, $value['stock'])
                ->setCellValue('G' . $j, $value['first_category'])
                ->setCellValue('H' . $j, $value['second_category'])
                ->setCellValue('I' . $j, $value['third_category']);
            $j ++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save("php://output");

    }

    /**
     * @Author  lingyun
     * @Desc    导出原料订单详情
     */
    public function export_material_order(){

    }

}